package where2go.sctu.com.zhiyou.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.entity.LineIntro;

/**
 * ProjectName: zhiyou
 * PackageName: where2go.sctu.com.zhiyou.fragment
 * Created by 张某人
 * CreatedTime:on 2018/5/8   00:33
 * Description:  线路详情简介
 */

public class LineIntroFragment extends Fragment {

    private TextView roadTitle;
    private TextView classIntro;
    private TextView roadContent;
    private TextView studyNum;
    private TextView coursePrice;

    private String title;
    private String content;
    private int num;
    private int price;

    private String classId="";

    private List<LineIntro.DataBean> dataList=new ArrayList<LineIntro.DataBean>();

   // Intent intent =getActivity().getIntent();

    private String urlDetail="http://47.104.80.155/youyiyou/class/aclassinfo?classId=";

    private static LineIntroFragment fa;
    public static LineIntroFragment getLineIntroFragment() {
        if (fa == null) {
            fa = new LineIntroFragment();
        }
        return fa;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.line_intro_fragment, null);

        //新页面接收数据
        classId=getArguments().getString("id");
        loaderData(urlDetail+classId);
        findView(view);
        return view;
    }

    private void findView(View view) {



        roadTitle=(TextView) view.findViewById(R.id.road_title);
        classIntro=(TextView) view.findViewById(R.id.class_intro);
        roadContent=(TextView) view.findViewById(R.id.road_content);
        studyNum=(TextView)view.findViewById(R.id.study_num);
        coursePrice=(TextView)view.findViewById(R.id.course_price);

    }

    private void loaderData(String url){

        StringRequest request=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson=new Gson();
                Type type=new TypeToken<LineIntro>(){}.getType();
                LineIntro lineIntro=gson.fromJson(response,type);
                LineIntro.DataBean  data=lineIntro.getData();
                title=data.getClassTitle();
                content=data.getClassDetail();
                num=data.getClassCount();
                price=data.getClassPrice();

                roadTitle.setText(title);
                roadContent.setText(content);
                studyNum.setText(num+"人学过");
                coursePrice.setText("￥ "+price);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.toString());
                Toast.makeText(getActivity(), "网络请求失败，请检查网络", Toast.LENGTH_SHORT).show();
            }
        });
        request.setTag("getLineIntro");
        MyApplication.getHttpQueues().add(request);
    }

    @Override
    public void onDestroy() {
        MyApplication.getHttpQueues().cancelAll("getLineIntro");
        super.onDestroy();
    }
}
